package cn.yh.register.hosp.service.impl;

import cn.yh.register.common.exception.RegisterException;
import cn.yh.register.common.result.ResultCodeEnum;
import cn.yh.register.hosp.mapper.HospitalSetMapper;
import cn.yh.register.hosp.service.HospitalSetService;
import cn.yh.register.model.hosp.HospitalSet;
import cn.yh.register.vo.order.SignInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    /**
     * 获取signKey
     *
     * @param hoscode
     * @return
     */
    @Override
    public String getSignKey(String hoscode) {
        HospitalSet hospitalSet = baseMapper.selectOne(new LambdaQueryWrapper<HospitalSet>().eq(HospitalSet::getHoscode, hoscode));
        if (hospitalSet == null) {
            throw new RegisterException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        return hospitalSet.getSignKey();
    }

    /**
     * 获取签名和路径信息
     *
     * @param hoscode 医院编号
     * @return SignInfoVo
     */
    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        HospitalSet hospitalSet =
                baseMapper.selectOne(new LambdaQueryWrapper<HospitalSet>().eq(HospitalSet::getHoscode, hoscode));
        if (hospitalSet != null) {
            SignInfoVo signInfoVo = new SignInfoVo();
            signInfoVo.setApiUrl(hospitalSet.getApiUrl());
            signInfoVo.setSignKey(hospitalSet.getSignKey());
            return signInfoVo;
        }
        return null;
    }
}
